import { WorkFlowApi } from '@/lib/comfyUI';
import { request } from './request';

// 创建图像
export interface CreateImageResult {
  prompt_id: string;
  number: 10;
  node_errors: {};
}

export function createImage<T = CreateImageResult>(url: string, prompt: WorkFlowApi) {
  return request<T, T>({
    url: url,
    method: 'POST',
    data: {
      prompt: prompt,
    },
  });
}

// 上传图像
export interface UpLoadImageResult {
  name: string;
  subfolder: string;
  type: 'input';
}
export function upLoadImage<T = UpLoadImageResult>(url: string, file: File) {
  // console.log(url, file);
  return request<T, T>({
    url: url,
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: {
      image: file,
    },
  });
}

// 获取生成图像的位置
export interface GetImageGenerationLocationResult {
  [key: string]: {
    prompt: [number, string, WorkFlowApi, { client_id: string }, string[]];
    outputs: {
      [nodeId: string]: {
        text?: string[];
        images?: Array<{
          filename: string;
          subfolder: string;
          type: string;
        }>;
      };
    };
    status: {
      status_str: string;
      completed: boolean;
      messages: Array<
        | ['execution_start', { prompt_id: string; timestamp: number }]
        | ['execution_cached', { nodes: string[]; prompt_id: string; timestamp: number }]
        | ['execution_success', { prompt_id: string; timestamp: number }]
      >;
    };
    meta: {
      [nodeId: string]: {
        node_id: string;
        display_node: string;
        parent_node: null;
        real_node_id: string;
      };
    };
  };
}
export async function getImageGenerationLocation<T = GetImageGenerationLocationResult>(
  url: string,
) {
  return request<T, T>({
    url: url,
    method: 'GET',
    headers: {
      // Referer: 'https://www.gigab2b.com/index.php?route=account/login',
    },
  });
}

// 下载图像
export function downloadImage<T = Blob>(url: string) {
  return request<T, T>({
    url: url,
    method: 'GET',
    responseType: 'blob',
  });
}
